package com.aaie.gru.response;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class ResMessage {

    private String resulttr;

    public static String ok(Object data,int pageIndex,int pageSize,int total){
        JSONObject headJson = new JSONObject();
        headJson.put("status",true);
        headJson.put("error_code",200);
        headJson.put("msg","success");
        headJson.put("page_index",pageIndex);
        headJson.put("page_size",pageSize);
        headJson.put("total",total);

        JSONObject resultJson = new JSONObject();
        if (data instanceof JSONObject && data != null){
            resultJson.put("body", (JSONObject) data);
        } else {
            resultJson.put("body", data);
        }
        resultJson.put("head",headJson);
        return JSONObject.toJSONString(resultJson, SerializerFeature.WriteNullStringAsEmpty);
    }

    public static String ok(Object data,long pageIndex,long pageSize,long total){
        JSONObject headJson = new JSONObject();
        headJson.put("status",true);
        headJson.put("error_code",200);
        headJson.put("msg","success");
        headJson.put("page_index",pageIndex);
        headJson.put("page_size",pageSize);
        headJson.put("total",total);

        JSONObject resultJson = new JSONObject();
        if (data instanceof JSONObject && data != null){
            resultJson.put("body", (JSONObject) data);
        } else {
            resultJson.put("body", data);
        }
        resultJson.put("head",headJson);
        return JSONObject.toJSONString(resultJson, SerializerFeature.WriteNullStringAsEmpty);
    }

    /**
     *  该方法为 滤掉没有用的对象返回 null 的字段
     * @param data
     * @param pageIndex
     * @param pageSize
     * @param total
     * @param idection  标识 在传递 实体进来后 否进行过滤  true 是  false 否
     * @return
     */
    public static String ok(Object data,int pageIndex,int pageSize,long total,boolean idection){
        JSONObject headJson = new JSONObject();
        headJson.put("status",true);
        headJson.put("error_code",200);
        headJson.put("msg","success");
        headJson.put("page_index",pageIndex);
        headJson.put("page_size",pageSize);
        headJson.put("total",total);

        JSONObject resultJson = new JSONObject();
        resultJson.put("head",headJson);

        if (data instanceof JSONObject && data != null && !idection){
            resultJson.put("body", ((JSONObject) data).toJSONString());
        } else if (idection){
            resultJson.put("body", data);
            return resultJson.toJSONString();
        }else {
            resultJson.put("body", data);
        }

        return JSONObject.toJSONString(resultJson, SerializerFeature.WriteNullStringAsEmpty);
    }

    public static String ok(Object obj){
        JSONObject headJson = new JSONObject();
        headJson.put("status",true);
        headJson.put("error_code",200);
        headJson.put("msg","success");
        headJson.put("page_index",0);
        headJson.put("page_size",0);
        headJson.put("total",0);


        JSONObject resultJson = new JSONObject();
        if (obj instanceof JSONObject && obj != null){
            resultJson.put("body", (JSONObject) obj);
        } else {
            resultJson.put("body", obj);
        }
        resultJson.put("head",headJson);
        return JSONObject.toJSONString(resultJson, SerializerFeature.WriteNullStringAsEmpty);
    }

    /**
     * 过滤null 字段
     * @param obj
     * @return
     */
    public static String ok(Object obj,boolean bool){
        JSONObject headJson = new JSONObject();
        headJson.put("status",true);
        headJson.put("error_code",200);
        headJson.put("msg","success");
        headJson.put("page_index",0);
        headJson.put("page_size",0);
        headJson.put("total",0);


        JSONObject resultJson = new JSONObject();
        if (obj instanceof JSONObject && obj != null){
            resultJson.put("body", (JSONObject) obj);
        } else {
            resultJson.put("body", obj);
        }
        resultJson.put("head",headJson);
        return JSONObject.toJSONString(resultJson);
    }


    public static String error(String message,int code){
        JSONObject headJson = new JSONObject();
        headJson.put("status",false);
        headJson.put("error_code",code);
        headJson.put("msg",message);

        JSONObject resultJson = new JSONObject();
        resultJson.put("head",headJson);
        return JSONObject.toJSONString(resultJson, SerializerFeature.WriteNullStringAsEmpty);
    }
    public String getResulttr() {
        return resulttr;
    }

    public void setResulttr(String resulttr) {
        this.resulttr = resulttr;
    }
}
